package winOrDie.graphics.maps;

/**
 * This public class defines different methods and variables to initialize and check {@link Map_Cell} matrices. This matrices are used in 
 * {@link Map_Board} objects
 */
public final class Map_Board_Factory 
{

	/**
	 * Maximum number of columns
	 */
	public static final int Max_Columns = 80;
	
	/**
	 * Maximum number of rows
	 */
	public static final int Max_Rows = 56;
	
	/**
	 * Default number of columns
	 */
	public static final int Default_Columns = 40; 
	
	/**
	 * Default number of rows
	 */
	public static final int Default_Rows = 28; 
	
	/**
	 * This public static method checks correct columns and rows number
	 * @param columns
	 * @param rows
	 * @throws Exception
	 */
	public static void check_Columns_And_Rows_Number(
			int columns, 
			int rows) 
	throws Exception
	{
		if ((columns < 1) || (rows < 1))
		{
			Exception exception = new Exception("Columns and rows have to be more than 0.");
			throw exception;
		}
		
		if ((columns > Max_Columns) || (rows > Max_Rows))
		{
			Exception exception = new Exception("Maximum columns = " + Max_Columns + " and maximum rows = " + Max_Rows);
			throw exception;
		}
		
		if ((columns % 2 != 0) || (rows % 2 != 0))
		{
			Exception exception = new Exception("Columns and rows have to be even numbers");
			throw exception;
		}
	}
	
	
	/**
	 * This public static method checks {@link Map_Cell} matrix passed by parameter and throws {@link Exception} if there is something wrong 
	 * @param mapCell_Matrix
	 * @throws Exception
	 */
	public static void check_Map_Cell_Matrix(Map_Cell mapCell_Matrix[][]) throws Exception
	{
		/*
		 * Check correct number of columns and rows
		 */
		int columns = mapCell_Matrix.length;
		int rows = mapCell_Matrix[0].length;
		check_Columns_And_Rows_Number(columns, rows);
		
		/*
		 * Check cell by cell if there is any problem
		 */
		for (int column = 0; column < mapCell_Matrix.length; column++) {
			for (int row = 0; row < mapCell_Matrix[column].length; row++) {
				if (mapCell_Matrix[column][row] == null)
				{
					Exception exception = new Exception("Map Cell at column " + column + " and row " + row + " is null.");
					throw exception;
				}
			}
		}
	}
	
	/**
	 * This public static method returns a {@link Map_Cell} matrix with the columns and rows passed by parameter
	 * @param columns
	 * @param rows
	 * @return {@link Map_Cell} matrix
	 * @throws Exception
	 */
	public static Map_Cell[][] get_MapCells(
			int columns, 
			int rows) 
	throws Exception
	{
		
		check_Columns_And_Rows_Number(columns, rows);
		
		/*
		 * Initialize matrix
		 */
		Map_Cell mapCell[][] = new Map_Cell[columns][rows];
		
		for (int column = 0; column < columns; column++) {
			for (int row = 0; row < rows; row++) {
				mapCell[column][row] = new Map_Cell();
			}
		}
		
		return mapCell;
	}
	
}
